package com.yykj.drup.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yykj.common.vo.ResultVO;
import com.yykj.drup.server.model.DeliveryAddress;
import com.yykj.drup.server.service.DeliveryAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @Author zeng
 * @Date 2022/3/21
 */
@Api(tags = "收货地址管理")
@RequestMapping("/api/drug/")
@AllArgsConstructor
@RestController
public class DeliveryAddressController {

    final DeliveryAddressService deliveryAddressService;

    @ApiOperation("新增收货地址")
    @PostMapping("/deliveryAddress/add")
    public ResultVO add(@RequestBody DeliveryAddress deliveryAddress){
        boolean b = deliveryAddressService.save(deliveryAddress);
        if (b){
            if(deliveryAddress.getIsDefault() == 1){
                deliveryAddressService.setDeliveryAddressDefault(deliveryAddress.getId());
            }
            return ResultVO.success();
        }
        return ResultVO.error("新增收货地址失败");
    }

    @ApiOperation("编辑收货地址")
    @PostMapping("/deliveryAddress/edit")
    public ResultVO edit(@RequestBody DeliveryAddress deliveryAddress){
        boolean b = deliveryAddressService.updateById(deliveryAddress);
        if (b && deliveryAddress.getIsDefault() == 1){
            deliveryAddress.setIsDefault(deliveryAddress.getId());
            return ResultVO.success();
        }
        return ResultVO.error("编辑收货地址失败");
    }

    @ApiOperation("删除收货地址")
    @GetMapping("/deliveryAddress/deleted")
    @ApiImplicitParam(name="id",value = "收货地址主键ID")
    public ResultVO deleted(@RequestParam(value = "id") Integer id){
        DeliveryAddress address = deliveryAddressService.getById(id);
        if (address == null){
            return ResultVO.error("数据不存在");
        }
        address.setIsDeleted(1);
        boolean b = deliveryAddressService.updateById(address);
        if (b){
            return ResultVO.success();
        }
        return ResultVO.error("删除收货地址失败");
    }

    @ApiOperation("获取小程序用户收货地址列表")
    @GetMapping("/deliveryAddress/findBySnsUserId")
    @ApiImplicitParam(name="snsUserId",value = "小程序用户标识")
    public ResultVO<DeliveryAddress> findBySnsUserId(@RequestParam(value = "snsUserId") Integer snsUserId){

        QueryWrapper<DeliveryAddress> wrapper = new QueryWrapper<>();
        wrapper
                .eq("sns_user_id",snsUserId)
                .eq("is_deleted",0)
                .orderByDesc("created_time");
        return ResultVO.success(deliveryAddressService.list(wrapper));
    }
    @ApiOperation("获取小程序用户默认收货地址")
    @GetMapping("/deliveryAddress/finddefault")
    @ApiImplicitParam(name="snsUserId",value = "小程序用户标识")
    public ResultVO<DeliveryAddress> finddefault(@RequestParam(value = "snsUserId") Integer snsUserId){

        QueryWrapper<DeliveryAddress> wrapper = new QueryWrapper<>();
        wrapper
                .eq("sns_user_id",snsUserId)
                .eq("is_default",1)
                .eq("is_deleted",0);
        return ResultVO.success(deliveryAddressService.getOne(wrapper) );
    }


}
